package com.freedy.concurrent.javaThread.state;


import lombok.extern.log4j.Log4j;

/**
 * 线程的状态
 * @author Freedy
 * @date 2021/4/20 16:42
 */
@SuppressWarnings("AlibabaAvoidManuallyCreateThread")
@Log4j
public class Test {
    public static void main(String[] args) throws Exception {
        Object lock = new Object();
        Thread t1 = new Thread(() ->{
            try {
                Thread.sleep(500);
                synchronized (lock){

                }
                while (!Thread.currentThread().isInterrupted());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, "t1");
        Thread t2 = new Thread(() -> {
            synchronized (lock) {
                while (!Thread.interrupted());
            }
            try {
                t1.join();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, "t1");
        t2.setDaemon(true);
        t2.start();

        log.info(t1.getState());

        t1.start();
        log.info(t1.getState());

        Thread.sleep(100);
        log.info(t1.getState());

        Thread.sleep(500);
        log.info(t1.getState());

        t2.interrupt();
        Thread.sleep(100);
        log.info(t2.getState());

        t1.interrupt();
        t1.join();
        log.info(t1.getState());
    }
}
